#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os
import time
import json
from pprint import pprint


def myprint(obj, end='\n'):
    sys.stdout.write(str(obj) + end)


def main(mondir, outfile):
    if os.path.isfile(outfile):
        if open(outfile, 'r').read().strip() == '':
            fsizedict = {}
        else:
            with open(outfile, 'r') as f:
                fsizedict = json.load(f)
    else:
        fsizedict = {}
    for (dirpath, dirnames, filenames) in os.walk(mondir):
        for filename in filenames:
            fname = os.path.join(dirpath.decode('gbk'),
                                 filename.decode('gbk'))
            fsize = os.stat(fname).st_size
            if fsizedict.get(fname, None) is None:
                fsizedict[fname] = [fsize]
            else:
                fsizedict[fname].append(fsize)

            # time.sleep(0.01)

    with open(outfile, 'w') as f:
        json.dump(fsizedict, f)


def cal(outfile):
    with open(outfile, 'r') as f:
        fsizedict = json.load(f)
    result = [(v[-1] - v[0], k) for (k, v) in fsizedict.items()
              if len(v) > 1]
    yousize = 5
    t = [x for x in result if x[0] > yousize]
    pprint(t)


if __name__ == '__main__':
    mondir = r'E:\Program Files\tdx'
    outfile = r'c:/temp/fsizedict.txt'
    if sys.argv[1] == 'mon':
        main(mondir, outfile)
    elif sys.argv[1] == 'cal':
        cal(outfile)
    myprint('done')
